package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.ProductPic;
import cn.stylefeng.guns.modular.system.mapper.ProductPicMapper;
import cn.stylefeng.guns.modular.system.model.params.ProductPicParam;
import cn.stylefeng.guns.modular.system.model.result.resultVO.ProductPicVO;
import cn.stylefeng.guns.modular.system.model.result.resultVO.ProductVO;
import cn.stylefeng.guns.modular.system.service.ProductPicService;
import cn.stylefeng.guns.sys.core.util.common.ResponseMsg;
import cn.stylefeng.guns.sys.core.util.common.SystemConstant;
import cn.stylefeng.guns.sys.core.util.common.UploadFileUtils;
import cn.stylefeng.guns.sys.core.util.common.UploadResult;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 产品图片表控制器
 *
 * @author 小飞
 * @Date 2019-06-27 14:38:08
 */
@Controller
@RequestMapping("/productPic")
public class ProductPicController extends BaseController {

    private String PREFIX = "/productPic";

    @Autowired
    private ProductPicService productPicService;
    @Autowired
    private ProductPicMapper productPicMapper;

    /**
     * 跳转到主页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/productPic.html";
    }

    /**
     * 新增页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/productPic_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/productPic_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ProductPicParam productPicParam) {

        List<String> picPath = Arrays.asList(productPicParam.getPicPath().split(","));
        picPath.forEach(s -> {
            ProductPic productPic = new ProductPic();
            BeanUtils.copyProperties(productPicParam,productPic);
            productPic.setPicPath(s);
            productPicService.save(productPic);
        });
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ProductPicParam productPicParam) {
        this.productPicService.update(productPicParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ProductPicParam productPicParam) {
        this.productPicService.delete(productPicParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ProductPicParam productPicParam) {
        ProductPic detail = this.productPicService.getById(productPicParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ProductPicParam productPicParam) {

        Page page = LayuiPageFactory.defaultPage();
        Page<ProductPicVO> pages = productPicMapper.customPageList(page,productPicParam);
        List<ProductPicVO> productPicVOS = pages.getRecords();
        ProductPicVO productPic = null;
        List<ProductPicVO> productVOS = new ArrayList<>();
        for(ProductPicVO productPicVO : productPicVOS){
            productPic = new ProductPicVO();
            BeanUtils.copyProperties(productPicVO,productPic);
            if (ToolUtil.isNotEmpty(productPicVO.getPicPath())){
            productPic.setPicPath(SystemConstant.TENCENT_URL.concat(productPicVO.getPicPath()));
            }
            productVOS.add(productPic);
        }
        pages.setRecords(productVOS);
        return LayuiPageFactory.createPageInfo(pages);
    }


    @RequestMapping(method = RequestMethod.POST, path = "/upload")
    @ResponseBody
    public ResponseData uploadUtils(@RequestPart("file") MultipartFile file) throws FileNotFoundException {

        if (ToolUtil.isEmpty(file)){
            return  ResponseData.error(201,"上传失败，图片为空");
        }else{

            ResponseMsg msg = UploadFileUtils.uploadFile(file);

            if (msg.getStatus() == 10001){
                UploadResult result = (UploadResult) msg.getResult();
                return  ResponseData.success(200,"上传成功",result.getPath());
            }else {
                return  ResponseData.error(202,"上传失败，图片格式错误");
            }

        }

    }


}


